import axios from './axios'
import dateFormat from './dateformat'
import rules from './validate'
import dictCode from './dictCode'

import LeSelect from '~/components/LeSelect'
import LeDialog from '~/components/LeDialog'

import Line from '~/components/Charts/Line'
import PieDoughnut from '~/components/Charts/PieDoughnut'
import LeaveCrud from '~/components/LeaveCrud'

function jsonFormat(obj) {
  if (!obj) {
    return
  }
  let json = JSON.stringify(obj, null, '\t')
  json = json.replace(/\n/g, '<br />')
  json = json.replace(/\t/g, '&nbsp;&nbsp;')
  return json
}

export default (Vue) => {
  Vue.prototype.$rules = rules
  // 装载 dictCode
  Vue.prototype.$dictCode = dictCode
  Vue.prototype.$httpGet = (url, params) => axios.get(url, { params })
  Vue.prototype.$httpPost = axios.post
  Vue.prototype.$httpPut = axios.put
  Vue.prototype.$httpDelete = axios.delete
  Vue.prototype.$axios = axios
  Vue.prototype.dateFormat = (date, model = 'yyyy-mm-dd HH:MM:ss') => dateFormat(date, model)
  Vue.prototype.$jsonFormat = jsonFormat
  Vue.filters = {
    dateFormat,
  }
  Vue.component(LeSelect.name, LeSelect)
  Vue.component(PieDoughnut.name, PieDoughnut)
  Vue.component(Line.name, Line)
  Vue.component(LeDialog.name, LeDialog)
  Vue.component(LeaveCrud.name, LeaveCrud)
  Vue.filter('date', date => {
    const day = 86400000
    const now = Date.now()
    const time = new Date(date).getTime()
    const cut = now - time
    if (cut > day) {
      return dateFormat(time, 'yyyy-mm-dd HH:MM:ss')
    } else {
      return dateFormat(time, 'HH:MM:ss')
    }
  })
}
